package com.dd.controller;

import com.dd.model.Customer;
import com.dd.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户控制器
 */
@RestController
@RequestMapping("/customers")
public class CustomerController {
    
    @Autowired
    private CustomerService customerService;
    
    /**
     * 获取所有客户
     */
    @GetMapping
    public ResponseEntity<List<Customer>> getAllCustomers() {
        List<Customer> customers = customerService.findAllCustomers();
        return ResponseEntity.ok(customers);
    }
    
    /**
     * 根据ID获取客户
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getCustomer(@PathVariable("id") Integer id) {
        Customer customer = customerService.getCustomerById(id);
        if (customer == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "客户不存在");
            return ResponseEntity.badRequest().body(response);
        }
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 根据邮箱获取客户
     */
    @GetMapping("/email")
    public ResponseEntity<?> getCustomerByEmail(@RequestParam("email") String email) {
        Customer customer = customerService.getCustomerByEmail(email);
        if (customer == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "客户不存在");
            return ResponseEntity.badRequest().body(response);
        }
        return ResponseEntity.ok(customer);
    }
    
    /**
     * 根据名称查询客户
     */
    @GetMapping("/search")
    public ResponseEntity<List<Customer>> searchCustomers(@RequestParam("name") String name) {
        List<Customer> customers = customerService.findCustomersByName(name);
        return ResponseEntity.ok(customers);
    }
    
    /**
     * 添加客户
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addCustomer(@RequestBody Customer customer) {
        Map<String, Object> response = new HashMap<>();
        
        // 检查邮箱是否已存在
        Customer existingCustomer = customerService.getCustomerByEmail(customer.getEmail());
        if (existingCustomer != null) {
            response.put("success", false);
            response.put("message", "该邮箱已被注册");
            return ResponseEntity.badRequest().body(response);
        }
        
        boolean result = customerService.addCustomer(customer);
        
        if (result) {
            response.put("success", true);
            response.put("message", "添加客户成功");
            response.put("customerId", customer.getCustomerId());
        } else {
            response.put("success", false);
            response.put("message", "添加客户失败");
        }
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 更新客户
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateCustomer(@PathVariable("id") Integer id, 
                                                             @RequestBody Customer customer) {
        Map<String, Object> response = new HashMap<>();
        
        // 设置ID
        customer.setCustomerId(id);
        
        // 检查邮箱是否已被其他客户使用
        Customer existingCustomer = customerService.getCustomerByEmail(customer.getEmail());
        if (existingCustomer != null && !existingCustomer.getCustomerId().equals(id)) {
            response.put("success", false);
            response.put("message", "该邮箱已被其他用户注册");
            return ResponseEntity.badRequest().body(response);
        }
        
        boolean result = customerService.updateCustomer(customer);
        
        if (result) {
            response.put("success", true);
            response.put("message", "更新客户成功");
        } else {
            response.put("success", false);
            response.put("message", "更新客户失败，客户可能不存在");
        }
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 删除客户
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteCustomer(@PathVariable("id") Integer id) {
        Map<String, Object> response = new HashMap<>();
        boolean result = customerService.deleteCustomer(id);
        
        if (result) {
            response.put("success", true);
            response.put("message", "删除客户成功");
        } else {
            response.put("success", false);
            response.put("message", "删除客户失败，客户可能不存在");
        }
        
        return ResponseEntity.ok(response);
    }
}